package com.binance.gateway.util;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by Fei.Huang on 2018/10/30.
 */
@Log4j2
public class FilterUtil {

    public static void badRequest(HttpServletResponse resp) throws ZuulException {
        resp.setStatus(HttpStatus.BAD_REQUEST.value());
        resp.setContentType("application/json");
        try {
            JSONObject obj = new JSONObject();
            obj.put("code", HttpStatus.BAD_REQUEST.value());
            obj.put("msg", "Access denied");
            resp.getOutputStream().write(obj.toJSONString().getBytes());
            resp.getOutputStream().flush();
            resp.getOutputStream().close();
        } catch (Exception e) {
            log.error("badrequest error:", e);
        }
        throw new ZuulException(HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST.value(), null);
    }
}